package com.tom.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tom.domain.KillCourse;
import com.tom.dto.KillParamDto;
import com.tom.query.KillCourseQuery;
import com.tom.service.IKillCourseService;
import com.tom.util.AjaxResult;
import com.tom.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {
    @Autowired
    public IKillCourseService killCourseService;

    /**
     * 执行秒杀
     */
    @RequestMapping(value="/kill",method= RequestMethod.POST)
    public AjaxResult kill(@RequestBody KillParamDto dto){
        killCourseService.kill(dto);
        return AjaxResult.me().setResultObj(dto.getId());
    }


    /**
     * 查询所有的发布的秒杀课程
     */
    @RequestMapping(value="/published/all",method= RequestMethod.GET)
    public AjaxResult selectPublished(){
        List<?> result = killCourseService.selectPublishedFromCache();
        return AjaxResult.me().setResultObj(result);
    }

    /**
     * 查询单个的发布的秒杀课程
     */
    @RequestMapping(value="/published/one/{id}",method= RequestMethod.GET)
    public AjaxResult selectPublishedOne(@PathVariable("id")Long id){
        KillCourse killCourse = killCourseService.selectPublishedOne(id);
        return AjaxResult.me().setResultObj(killCourse);
    }

    /**
     * 保存秒杀
     */
    @RequestMapping(value="/add",method= RequestMethod.POST)
    public AjaxResult add(@RequestBody KillCourse killCourse){
        killCourseService.add(killCourse);
        return AjaxResult.me();
    }
    /**
    * 保存和修改
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{
            killCourseService.insert(killCourse);
        }
        return AjaxResult.me();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return AjaxResult.me();
    }

   /**
   * 根据ID获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("id")Long id){
        return AjaxResult.me().setResultObj(killCourseService.selectById(id));
    }


    /**
    * 获取所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public AjaxResult list(){
        return AjaxResult.me().setResultObj(killCourseService.selectList(null));
    }

    /**
    * 分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public AjaxResult json(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        PageList pageList = new PageList<KillCourse>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }
}
